package reconciliation

import (
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/view"

	"github.com/gin-gonic/gin"
)

func List(c *gin.Context) {

	requestDataMap := make(map[string]interface{})
	c.ShouldBindJSON(&requestDataMap)

	where, page, pageSize := controller.WhereStructure(requestDataMap)

	//查询信息
	var reconciliationViewStruct view.ReconciliationViewStruct
	var reconciliationViewData []view.ReconciliationViewStruct
	model.GetModelData(
		model.Db,
		reconciliationViewStruct.TableName(),
		&reconciliationViewData,
		model.QueryOptions{
			Page:       page,
			PageSize:   pageSize,
			Conditions: where,
			OrderBy:    "reconciliation_id desc",
			Debug:      true,
		},
	)

	//数据格式化
	returnData := make([]map[string]interface{}, 0)
	for _, v := range reconciliationViewData {
		returnData = append(returnData, map[string]interface{}{
			"id":            v.ReconciliationId,
			"file_path":     v.ReconciliationFilePath,
			"park_code":     v.ParkingLotParkCode,
			"park_name":     v.ParkingLotName,
			"status":        v.ReconciliationStatus,
			"created_at":    v.ReconciliationCreatedAt.Format("2006-01-02 15:04:05"),
			"type":          v.ReconciliationType,
			"out_file_path": v.ReconciliationOutFilePath,
		})
	}

	//总数查询
	count, _ := model.CountModelData(
		model.Db,
		reconciliationViewStruct.TableName(),
		model.QueryOptions{
			Conditions: where,
		},
	)

	controller.Success(c,
		map[string]interface{}{
			"list":        returnData,
			"total":       count,
			"pageSize":    pageSize,
			"currentPage": page,
		})
}
